const mixins = require('../../mixins/shop')
const common = require('../../mixins/common')
const app = getApp()
const options = {

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    page: 1,
    exchangeModalActive: false,
    exchangeModalVisible: false,
    exchangeModalActive: false,
    addressList: [],
    cart_num: 1,
    model: [],
    message: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.setData({
        id: options.id
      }),
      this.getproductDetail();
  },

  buy: function () {
    this.setData({
      exchangeModalHidden: false,
      exchangeModalActive: true,
      exchangeModalVisible: true,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  change:function(e){
    console.log(e);
    this.setData({
      current:e.detail.current
    })

  },

  /*
   * 导入微信地址
   */
  getWxAddress: function () {
    var that = this;
    wx.authorize({
      scope: 'scope.address',
      success: function (res) {
        wx.chooseAddress({
          success: function (res) {
            var addressP = {};
            addressP.province = res.provinceName;
            addressP.city = res.cityName;
            addressP.district = res.countyName;

            that.data.addressList = {
              address: addressP,
              is_default: 1,
              real_name: res.userName,
              post_code: res.postalCode,
              phone: res.telNumber,
              detail: res.detailInfo,
              id: 0
            };
            that.setData({
              addressList: that.data.addressList,
            })
            wx.setStorageSync('addressList', that.data.addressList)
          },
          fail: function (res) {
            if (res.errMsg == 'chooseAddress:cancel') return
            wx.showToast({
              title: '获取地址失败',
            })
          },
        })
      },
      fail: function (res) {
        wx.showModal({
          title: '您已拒绝导入微信地址权限',
          content: '是否进入权限管理，调整授权？',
          success(res) {
            if (res.confirm) {
              wx.openSetting({
                success: function (res) {
                  console.log(res.authSetting)
                }
              });
            } else if (res.cancel) {
              return app.Tips({
                title: '已取消！'
              });
            }
          }
        })
      },
    })
  },

  hideExchangeModal: function () {
    this.setData({
      exchangeModalHidden: true,
      exchangeModalActive: false,
      exchangeModalVisible: false,
    })
  },

  backIndex:function(){
  wx.reLaunch({
    url: '/pages/tabbar/index/index',
  })
  },

  bindMessage: function (e) {
    if (e.detail.value.length > 0) {
      this.setData({
        message: e.detail.value
      })
    }
  },


  tapAttr: function (e) {
    //父级index
    var indexw = e.currentTarget.dataset.indexw;
    //子集index
    var indexn = e.currentTarget.dataset.indexn;
    //每次点击获得的属性
    var attr = this.data.detail.product.model[indexw].option[indexn];
    console.log(attr);
    //设置当前点击属性
    this.data.detail.product.model[indexw].checked = attr;
    this.data.model[indexw] = this.data.detail.product.model[indexw].name + ":" + attr;
    this.setData({
      model: this.data.model,
      detail: this.data.detail,
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let user = app.globalData.user
    if (!user) {
      user = '';
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    this.setData({
      user: user,
    })
    var addressList = wx.getStorageSync('addressList');
    if (addressList) {
      this.setData({
        addressList: addressList
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}

mixins(options)
common(options)
Page(options)